<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.index');
    }

    public function info()
    {
        return view('admin.info');
    }
//    修改密码
    public function pass()
    {
        return view('admin.pass');
    }

    //修改密码逻辑
    public function dopass()
    {

      // 1 获取用户提交过来的数据
       $input = Input::except('_token');
      // 2 做表单验证
        $rule = [
            'password_o'=>'required|between:5,12',
            'password'=>'required|between:6,18',
            'password_c'=>'required|same:password'
        ];

        $mess = [
            'password_o.required'=>'原密码必须输入',
            'password_o.between'=>'原密码的长度在5-12位之间',
            'password.required'=>'新密码必须输入',
            'password.between'=>'新密码的长度在6-18位之间',
            'password_c.required'=>'确认密码必须输入',
            'password_c.same'=>'确认密码跟新新密码必须一致',
        ];

        $validator = Validator::make($input, $rule,$mess);
//        如果验证失败
        if($validator->fails()){
            return back()
                ->withErrors($validator)
                ->withInput();
        }
      // 3 修改密码

       $user =  User::find(session('user')->user_id);
        $user->user_pass = Crypt::encrypt($input['password']);
       $re =  $user->save();
       if($re){
           return back()->with('errors','修改成功');
       }else{
           return back()->with('errors','修改失败');
       }
    }
    
//    退出登录
    public function logout()
    {
//        session(['user'=>null]);

        session()->flush();
        return redirect('admin/login');
    }
}
